#!/usr/bin/env python
# -*- coding: utf-8 -*-
#       models.py##       Copyright 2010 vvs <skochko@gmail.com>##       This program is free software; you can redistribute it and/or modify#       it under the terms of the GNU General Public License as published by#       the Free Software Foundation; either version 2 of the License, or#       (at your option) any later version.##       This program is distributed in the hope that it will be useful,#       but WITHOUT ANY WARRANTY; without even the implied warranty of#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the#       GNU General Public License for more details.##       You should have received a copy of the GNU General Public License#       along with this program; if not, write to the Free Software#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,#       MA 02110-1301, USA.

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import Column, Integer, String, Date, MetaData, Boolean, Float
import os

DB_FILE = 'tolerances.db'

engine = create_engine('sqlite:///%s' % DB_FILE, echo=True)
Base = declarative_base()
Session = scoped_session(sessionmaker(bind=engine))

def init_db():
    """
    Создание БД.
    """

    Base.metadata.create_all(engine)

def exist_db():
    return os.path.exists(DB_FILE)

class Tolerance(Base):
    """
    Модель Допусков
    """
    #name; min_dim; max_dim; up_tolerance; un_tolerance; hole; shaft; recomended'
    __tablename__ = 'Tolerances'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    min_dim = Column(Float)
    max_dim = Column(Float)
    up_tolerance = Column(Float)
    un_tolerance = Column(Float)
    hole = Column(Boolean)
    shaft = Column(Boolean)
    recomended = Column(Boolean)

    def __init__(self, name, min_dim, max_dim, up_tolerance, un_tolerance, hole, shaft, recomended):
        self.name = name
        self.min_dim = min_dim
        self.max_dim = max_dim
        self.up_tolerance = up_tolerance
        self.un_tolerance = un_tolerance
        self.hole = hole
        self.shaft = shaft
        self.recomended = recomended

    def __repr__(self):
        return "<Metadata('%s')>" % (self.name)
